﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MachineBuildingSystem
{
    public partial class ProMasterDataForm : XtraForm
    {

        private DataTable  m_SourceDT =new DataTable ();

        private List<ProductDataInfo> m_SelectedProductDataList = new List<ProductDataInfo>();
        public List<ProductDataInfo> SelectedProductDataList
        {
            get { return m_SelectedProductDataList; }
        }

        public ProMasterDataForm(DataTable dt)
        {
            m_SourceDT = dt;
            InitializeComponent();
        }

        private void ProMasterDataForm_Load(object sender, EventArgs e)
        {
            RefreshData();
        }

        private DataTable m_BindDT = null;

        private void InitBindDT()
        {
            m_BindDT = new DataTable();
            m_BindDT.Columns.Add("Checked", typeof(bool));
            m_BindDT.Columns.Add("prodno");
            m_BindDT.Columns.Add("pdate");
            m_BindDT.Columns.Add("carCode");
            m_BindDT.Columns.Add("driverName");
            m_BindDT.Columns.Add("volume");
        }

        private  void RefreshData()
        {
            try
            {
                InitBindDT();
                gdcProduct.DataSource = m_BindDT;
                foreach (DataRow dr in m_SourceDT.Rows)
                {
                    m_BindDT.Rows.Add(false, dr["prodno"].ToString(),
                        dr["pdate"].ToString(), dr["TruckCode"].ToString(),
                        dr["DriverName"].ToString(), dr["Volume"].ToString());
                }
                gdcProduct.DataSource = m_BindDT;
            }
            catch (Exception ex)
            {
                LogHelper.error("绑定订单列表异常：" + ex.ToString());
            }     
        }

        //只能勾选一个
        //选消耗时可以勾选多条数据并判断发货数量要和勾选的总生产数量相等
        private void repositoryItemCheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            //DataTable dt = this.gdcProduct.DataSource as DataTable;
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    dt.Rows[i]["Checked"] = false;
            //}
            //gdcProduct.RefreshDataSource();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            List<DataRow> selectedRowList = new List<DataRow>();
            for (int i = 0; i < gdvProduct.RowCount; i++)
            {
                DataRow row = gdvProduct.GetDataRow(i);
                if (Convert.ToBoolean(row["Checked"]) == true)
                    selectedRowList.Add(row);
            }
            if (selectedRowList.Count == 0)
            {
                MessageBox.Show(this, "请勾选发货时生产单据!");
                return;
            }

            foreach (DataRow row in selectedRowList)
            {
                ProductDataInfo data = new ProductDataInfo();
                data.Prodno = long.Parse(row["prodno"].ToString());
                data.Volume = decimal.Parse(row["Volume"].ToString());
                data.pdate = Convert.ToDateTime(row["pdate"].ToString());
                m_SelectedProductDataList.Add(data);
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}
